package com.snail.xgcd.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.snail.xgcd.bean.Constant;
import com.snail.xgcd.bean.Member;
import com.snail.xgcd.bean.Single;
import com.snail.xgcd.mapper.MemberMapper;
import com.snail.xgcd.service.MemberService;
import com.snail.xgcd.utils.id.IdWorkerBuider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceimpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Single findAll(Member member,Integer pageSize,Integer pageNum) {
        Single single = new Single();
        try{
            if (pageNum != null && pageSize != null){
                PageHelper.startPage(pageNum, pageSize);
            }
            List<Member> memberList = memberMapper.findAll(member);
            PageInfo page = new PageInfo(memberList);
            single.setData(memberList);
            single.setCount(page.getTotal()+"");
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage("获取会员列表成功！");
        }catch(Exception e){
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("获取会员列表失败！");
            e.printStackTrace();
        }
        return single;
    }

    @Override
    public Single deleteByPrimaryKey(Long id) {
        Single single = new Single();
        try{
            memberMapper.deleteByPrimaryKey(id);
            single.setData(id);
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage("删除会员成功！");
        }catch(Exception e){
            e.printStackTrace();
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("删除会员失败！");
        }
        return single;
    }

    @Override
    public Single insert(Member record) {
        Single single = new Single();
        try{
            record.setId(IdWorkerBuider.IDWORKER.nextId());
            record.setCreateDate(new Date());
            memberMapper.insert(record);
            single.setData(record.getId());
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage("添加会员成功!");
        }catch(Exception e){
            e.printStackTrace();
            single.setData(record.getId());
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("添加会员失败!");
        }
        return single;
    }

    @Override
    public int insertSelective(Member record) {
        return memberMapper.insertSelective(record);
    }

    @Override
    public Member selectByPrimaryKey(Long id) {
        return memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public Single updateByPrimaryKeySelective(Member record) {
        Single single = new Single();
        try{
            memberMapper.updateByPrimaryKeySelective(record);
            single.setData(record.getId());
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage("修改会员成功!");
        }catch(Exception e){
            e.printStackTrace();
            single.setData(record.getId());
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("修改会员失败!");
        }
        return single;
    }

    @Override
    public int updateByPrimaryKey(Member record) {
        return memberMapper.updateByPrimaryKey(record);
    }

    @Override
    public Single charge(String id ,String money) {
        Single single = new Single();
        try{
            Member member = memberMapper.selectByPrimaryKey(Long.parseLong(id));
            if(member != null){
                BigDecimal balance = member.getBalance().add(new BigDecimal(money));
                member.setBalance(balance.setScale(2, BigDecimal.ROUND_HALF_UP));
                memberMapper.updateByPrimaryKeySelective(member);
                single.setData(member);
                single.setCode(Constant.SUCCESS[0]);
                single.setMessage("充值成功!当前余额为¥"+balance);
            }else{
                single.setData(id);
                single.setCode(Constant.FAILURE[0]);
                single.setMessage("无此会员,请核实!");
            }
        }catch(Exception e){
            e.printStackTrace();
            single.setData(id);
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("充值失败!");
        }
        return single;
    }

    @Override
    public Single consume(String id ,String money) {
        Single single = new Single();
        try{
            Member member = memberMapper.selectByPrimaryKey(Long.parseLong(id));
            if(member != null){
                BigDecimal balance = member.getBalance()
                        .subtract(new BigDecimal(money))
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
                int i=balance.compareTo(BigDecimal.ZERO);
                int j=balance.compareTo(new BigDecimal(30));

                if(i>=0){
                    member.setBalance(balance);
                    memberMapper.updateByPrimaryKeySelective(member);
                    single.setData(member);
                    single.setCode(Constant.SUCCESS[0]);
                    single.setMessage(j > -1 ? "扣费成功!当前余额为"+balance:"扣费成功!当前余额为¥"+balance+",余额小于¥30,请提醒充值!");
                }else{
                    single.setData(member);
                    single.setCode(Constant.FAILURE[0]);
                    single.setMessage("余额不足,请充值!当前余额为"+member.getBalance()+"最低充值¥"+member.getBalance().subtract(new BigDecimal(money)).abs()+"才能完成本次消费!");
                }
            }else{
                single.setData(id);
                single.setCode(Constant.FAILURE[0]);
                single.setMessage("无此会员,请核实!");
            }
        }catch(Exception e){
            e.printStackTrace();
            single.setData(id);
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("扣费失败!");
        }
        return single;
    }
}
